<?php
/**
 * Created by zhangyh.
 * User: zhangyh
 * Date: 2018/11/5
 * Time: 17:00
 */

namespace App\Http\Controllers\Api\V1;


use App\Http\Controllers\Api\BaseController;
use App\Model\Record;
use App\Service\Activity\ActivityService;
use App\Service\Activity\RecordService;
use Illuminate\Http\Request;
use Validator;

class ActivityController extends BaseController
{


    /**
     * 添加活动
     * @param Request $request
     * @author 张永辉 2018年11月5日
     */
    public function attendActivity(Request $request, ActivityService $activityService)
    {
        $input = $request->input();
        $rule  = [
            'activity_id' => 'required',
            'img_ids'     => 'required',
        ];

        $validator = Validator::make($input, $rule, []);
        if ($validator->fails()) {
            error($validator->errors()->first());
        }
        $res = $activityService->setParams($input)->attendActivity();
        $res['errCode'] && error($res['errMsg']);
        success();

    }

    /**
     * 获取单个人的活动信息
     * @param Request $request
     * @author 张永辉 2018年11月5日
     */
    public function getRecord(Request $request, ActivityService $activityService)
    {
        $input     = $request->input();
        $rule      = [
            'activity_id' => 'required',
        ];
        $validator = Validator::make($input, $rule, []);
        if ($validator->fails()) {
            error($validator->errors()->first());
        }
        $res = $activityService->setParams($input)->getRecord();
        $res['errCode'] && error($res['errMsg']);
        success($res['data']);
    }


    /**
     * 获取单个人的活动信息
     * @param Request $request
     * @author 张永辉 2018年11月5日
     */
    public function recommend(Request $request, ActivityService $activityService)
    {
        $input     = $request->input();
        $rule      = [
            'activity_id' => 'required',
        ];
        $validator = Validator::make($input, $rule, []);
        if ($validator->fails()) {
            error($validator->errors()->first());
        }
        if (empty($input['page']) || $input['page'] == 1) {
            $data['activityData'] = $activityService->setParams($input)->getActivityById();
        }
        $data['record'] = $activityService->setParams($input)->recommend();
        success($data);
    }


    /**
     * 排行榜
     * @param Request $request
     * @author 张永辉 2018年11月6日
     */
    public function ranking(Request $request, RecordService $recordService)
    {
        $input = $request->input();
        $res   = $recordService->setParams($input)->ranking();
        success($res);
    }

    /**
     * 活动详情
     * @param Request $request
     * @author 张永辉 2018年11月6日
     */
    public function recordInfo(Request $request,RecordService $recordService)
    {
        $input     = $request->input();
        $rule      = [
            'id' => 'required',
        ];
        $validator = Validator::make($input, $rule, []);
        if ($validator->fails()) {
            error($validator->errors()->first());
        }
        if (empty($input['page']) || $input['page'] == 1){
            $data['record'] = $recordService->setParams($input)->recordInfo();
        }
        $data['like'] = $recordService->setParams($input)->recordLike();
        success($data);
    }


    /**
     * 参与记录
     * @author 张永辉 2018年11月6日
     */
    public function attendLog(Request $request,RecordService $recordService)
    {
        $input = $request->input();
        $data = $recordService->setParams($input)->attendLog();
        success($data);
    }


}